//
//  HLCustomAttachmentDecoder.m
//  YueYue
//
//  Created by hulu001 on 2023/6/12.
//

#import "HLCustomAttachmentDecoder.h"
#import "HLPushAttachment.h"
#import "HLGiftAttachment.h"
#import "HLVideoGuideAttachment.h"
#import "HLRechargeAttachment.h"
#import "HLAccostAttachment.h"
#import "HLPayRemindAttachment.h"
#import "HLRtcRubbishAttachment.h"
#import "HLOtherRemidAttach.h"

@implementation HLCustomAttachmentDecoder


- (id<NIMCustomAttachment>)decodeAttachment:(NSString *)content
{
    id<NIMCustomAttachment> attachment = nil;

    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    
    if (data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
                                                             options:0
                                                               error:nil];
        if ([dict isKindOfClass:[NSDictionary class]]) {
            
            NSInteger type = 0;
            id object = [dict objectForKey:@"type"];
            if ([object isKindOfClass:[NSString class]] ||
                [object isKindOfClass:[NSNumber class]])
            {
                type = [object integerValue];
            }
            if (type == 0) {
                id object = [dict objectForKey:@"msgType"];
                if ([object isKindOfClass:[NSString class]] ||
                    [object isKindOfClass:[NSNumber class]])
                {
                    type = [object integerValue];
                }
            }
            
            NSDictionary *body = dict[@"body"];
            if (body == nil){
                body = dict[@"ext"];
            }
            
            switch (type) {
                case 1:// 官方号
                {
                    attachment = [HLPushAttachment mj_objectWithKeyValues:body];
                }
                    break;
                case 2: // 余额不足
                {
                    attachment = [HLRechargeAttachment mj_objectWithKeyValues:body];
                }
                    break;
                case 9:// 搭讪
                {
                    attachment = [HLAccostAttachment mj_objectWithKeyValues:body];
                }
                    break;
                case 5: // IM免费消息用完的提醒
                {
                    attachment = [HLPayRemindAttachment mj_objectWithKeyValues:body];
                }
                    break;
                case 3: // 礼物消息
                case 4: // 礼物消息
                case 105:
                {
                    attachment = [HLGiftAttachment mj_objectWithKeyValues:body];
                }
                    break;
                case 6: // 提醒去呼叫视频
                {
                    attachment = [HLVideoGuideAttachment mj_objectWithKeyValues:body];
                }
                    break;
                case 10:
                case 11:
                case 12:
                {
                    attachment = [HLRtcRubbishAttachment mj_objectWithKeyValues:dict];
                }
                    break;
                case 13: // 广场点赞消息
                case 14: // 广场礼物消息
                case 15: // 广场评论消息
                {
                    
                }
                    break;
                case 16:
                case 17:
                {
                    HLOtherRemidAttach *a = [HLOtherRemidAttach mj_objectWithKeyValues:body];
                    a.msgType = type;
                    attachment = a;
                }
                    break;
                case 18:
                {
                    HLOtherRemidAttach *a = [HLOtherRemidAttach mj_objectWithKeyValues:body];
                    a.msgType = type;
                    a.tittle =[NSString stringWithFormat:@"对方免费聊天还剩%@条", a.msgCount];
                    a.content = @"";
                    attachment = a;
                }
                    break;
                case 19:
                {
                    HLOtherRemidAttach *a = [HLOtherRemidAttach mj_objectWithKeyValues:body];
                    a.msgType = type;
                    attachment = a;
                }
                    break;
                case 20: // HIChat消息用完的提醒
                {
                    HLOtherRemidAttach *a = [HLOtherRemidAttach mj_objectWithKeyValues:body];
                    a.msgType = type;
                    attachment = a;
                }
                    break;
                case 21: // HIChat消息用完的提醒
                {
                    HLOtherRemidAttach *a = [HLOtherRemidAttach mj_objectWithKeyValues:body];
                    a.msgType = type;
                    a.messageContent = @"恭喜您升级了";
                    attachment = a;
                }
                    break;
                case 22: // HIChat消息用完的提醒
                {
                    HLAccostAttachment *a = [HLAccostAttachment mj_objectWithKeyValues:body];
                    attachment = a;
                }
                    break;
                default:
                    break;
            }
        }
    }
    return attachment;
}
@end
